<template>
	<view class="content">
		<view class="login-bg">
<!-- 			<view class="header">
				<view class="goBack" @click="goBack">
				</view>
			</view> -->
			<view class="titleText">
				<view class="hy">欢迎您！(✿◡‿◡)</view>
				<view class="xccp"></view>
			</view>
			<view class="login-card">
<!-- 				<view class="uni-media-image">
					<image class="uni-media-loading" src="/static/theme/default/app.png" />
				</view> -->
				<!-- <view class="login-head">&nbsp;&nbsp;</view> -->
				<label class="uni-list-cell uni-list-cell-pd" style="padding: 0;border-bottom: 1px #e7e6ef solid;margin-top: 60upx;">
					<!-- <label class="label-2-text" >
					   <text>帐号</text>
					</label> -->
					<view >
						<input type="number" class="uni-input"  placeholder="手机号码" v-model="form.username" />
					</view>
					<view class="uni-icon uni-icon-clear" v-if="form.username" @click="delInputUsernameText"></view>
					<view class="uni-icon" v-else ></view>
					<view  class="uni-icon placeholdertext">&nbsp;</view>
				</label>
				<label class="uni-list-cell uni-list-cell-pd" style="padding: 0;border-bottom: 1px #e7e6ef solid;margin-top: 60upx;" >
						<!-- <label class="label-2-text" >
						   <text>密码</text>
						</label> -->				   
						<view >
						   <input class="uni-input" placeholder="密码" :password="showPassword" v-model="form.password"/>
						</view>
						
						<view class="uni-icon uni-icon-clear" v-if="form.password" @click="delInputPasswordText"></view>
						<view class="uni-icon" v-else ></view>
						<view class="uni-icon uni-icon-eye" :class="[showPassword ? '' : 'uni-active']" @click="changePassword"></view>
						
				</label>
		</view>
		<view class="reset">
			<view class="" @click="resetPwd">重置密码</view>
		</view>
		<view class="login-btn">
			<button :class="['landing',checkIn ? 'landing_true' : 'landing_false']" 
			:disabled="checkIn ? false : true" type="primary" @tap="subLongin">登 录</button> 
			<!--  <view :class="['landing',checkIn ? 'landing_true' : 'landing_false']" @tap="subLongin">登 陆</view> -->
		</view>
		<view class="login-function-old">
			<!-- <view class="login-forget" @click="go_forget">忘记密码</view> -->
			<!-- <view class="login-register" @click="go_register">还没有账号，前往注册</view> -->
		</view>
		<!--
		<view>
			<view class="login-function">
				
					<text class="textspace" @click="go_forget">找回密码</text> |
				    <text class="textspace" @click="go_frozen">紧急冻结</text> |
					<text class="textspace" @click="go_register">快速注册</text>
				  
			
			</view>
		</view>
		-->
		<u-popup :round="10" mode="center" zIndex="99999" :show="pwdVisible" @close="pwdVisible = false" @cancel="pwdVisible = false">
			<view class="pwd-box">
				<view class="pwd-box__header" v-if="type == 0">
					您的密码过于简单，请修改密码
				</view>
				<view class="uni-form-item uni-column pwd-box-item" v-if="type == 2">
					<view class="pwd-box-item-title">输入待重置手机号：</view>
					<view class="with-fun">
						<input class="uni-input pwd-input" placeholder="请输入手机号" v-model="phone" />
					</view>
				</view>
				<view class="uni-form-item uni-column pwd-box-item">
					<view class="pwd-box-item-title">输入新密码：</view>
					<view class="with-fun">
						<input class="uni-input pwd-input" placeholder="请输入密码" :password="false" v-model="formData.pass2" />
					</view>
				</view>
				<view class="uni-form-item uni-column pwd-box-item">
					<view class="pwd-box-item-title">确认新密码：</view>
					<view class="with-fun">
						<input class="uni-input pwd-input" placeholder="请输入密码" :password="false" v-model="formData.pass3" />
					</view>
				</view>
			</view>
			<u-button text="确定" @click="confimPwd" color="#8db9ca"></u-button>
		</u-popup>
	  </view>
   </view>
</template>

<script>
	import _data from '../../common/_data.js'
	import _custom from '../../common/_custom.js'
	
	export default {
		data() {
			return {
				showPassword: true,
				form: {
					username: _data.localData('username'),
					password: _data.localData('password'),
					clientid: _data.localData('register_id'),
				},
				phone: '',
				pwdVisible: false,
				formData: {
					pass1: '',
					pass2: '',
					pass3: '',
				},
				type: 0,//0：修改密码简单，1：重置密码(不带手机号)，2：重置密码(带手机号)
				preResetData: {},
			}
		},
		onLoad() {
			let isLogOut = _data.localData('isLogOut');
			if(isLogOut == 2) {
				//this.subLongin();
			}
		},
		computed: {
			checkIn(){
				return this.form.password != '' && this.form.username != '' && this.form.password.length > 5;
			}
		},
		methods: {
			goBack(){
				uni.navigateBack()
			},
			changePassword() {
				this.showPassword = !this.showPassword;
			},
			delInputUsernameText(){
				this.form.username = ''
			},
			delInputPasswordText(){
				this.form.password = ''
			},
			confimPwd() {
				let _this = this;
				if(!_this.formData.pass2 || _this.formData.pass2.length < 6){
					uni.showToast({
						title: '请输入新密码,不能小于6位',
						icon: 'none'
					})
					return;
				}
				if(!_this.formData.pass3 || _this.formData.pass3.length < 6){
					uni.showToast({
						title: '请确认新密码,不能小于6位',
						icon: 'none'
					})
					return;
				}
				if(_this.formData.pass2 !== _this.formData.pass3){
					uni.showToast({
						title: '两次新密码不一致',
						icon: 'none'
					})
					return;
				}
				if(_this.formData.pass2 == '123456') {
					uni.showToast({
						title: '密码过于简单',
						icon: 'none'
					})
					return;
				}
				_this.formData.pass1 = _this.form.password;
				console.log(this.type)
				switch(this.type) {
					case 0:
						_this.$httpSend({
							path: '/im/set/password',
							data: _this.formData,
							success() {
								_custom.getDataJava({
									apiname: 'updateUserPassword',
									code: 2,
									apiparam: {
										obsusername: _data.localData('name'),
										pwd: _this.formData.pass2,
										shouji: _data.localData('phone')
									},
									success() {
										uni.reLaunch({
											url: '../home/index',
										});
									}
								})
							}
						});
						break;
					case 1:
						_this.$httpSend({
							path: '/im/in/resetPwd',
							data: {
								..._this.formData,
								client_id: _this.form.clientid
							},
							success(res) {
								console.log('1111',res)
								//设置token
								_this.$httpJavaSend({
									data: {
										apiname: 'userLOGIN',
										token: '',
										code: 100,
										apiparam: {
											usertab: 'OBSLIST',
											username: '1880000088',
											password: '1880000088@15'
										}
									},
									success(data) {
										if(data.msgcode == 0) {
											_data.localData('obs_token',data.token)
											_data.localData('obscode',data.obscode)
											//重置EOD密码
											_custom.getDataJava({
												apiname: 'updateUserPassword',
												code: 2,
												apiparam: {
													obsusername: _this.preResetData.obsusername,
													pwd: _this.formData.pass2,
													shouji: _this.preResetData.shouji
												},
												success(r) {
													uni.showToast({
														title:'修改成功!',
														icon: 'none'
													})
													_this.pwdVisible = false;
													_this.form.password = _this.formData.pass2;
													_this.showPassword = false;
													_this.type = 0;
												},
											})
										}
									}
								})
							}
						});
						break;
					case 2:
						//验证
						_this.$httpSend({
							path: '/im/in/preResetPwdCheck',
							data: {
								client_id: _this.form.clientid,
								username: _this.phone
							},
							success(e) {
								switch(e.num) {
									case 1:
										_this.$httpSend({
											path: '/im/in/resetPwd',
											data: {
												..._this.formData,
												client_id: _this.form.clientid,
											},
											success(res) {
												//设置token
												_this.$httpJavaSend({
													data: {
														apiname: 'userLOGIN',
														token: '',
														code: 100,
														apiparam: {
															usertab: 'OBSLIST',
															username: '1880000088',
															password: '1880000088@15'
														}
													},
													success(data) {
														if(data.msgcode == 0) {
															_data.localData('obs_token',data.token)
															_data.localData('obscode',data.obscode)
															//重置EOD密码
															_custom.getDataJava({
																apiname: 'updateUserPassword',
																code: 2,
																apiparam: {
																	obsusername: e.obsusername,
																	pwd: _this.formData.pass2,
																	shouji: e.shouji
																},
																success(r) {
																	uni.showToast({
																		title:'修改成功!',
																		icon: 'none'
																	})
																	_this.pwdVisible = false;
																	_this.form.password = _this.formData.pass2;
																	_this.showPassword = false;
																	_this.type = 0;
																},
															})
														}
													}
												})
											}
										});
										break;
									case 0:
										uni.showToast({
											title: '手机号与设备绑定校验失败',
											icon: 'none'
										})
										break;
									default:
										uni.showToast({
											title: '校验失败',
											icon: 'none'
										})
										break;
								}
							}
						})
						break;
				}
			},
			subLongin(){
				
				let _this = this;
				if(!_this.checkIn){
					return;
				}
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(_this.form.username))){ 
					uni.showToast({
						title: "手机号码有误，请重填",
						icon: 'error'
					})
					return; 
				}
				uni.showLoading({
					title: '登录中，请稍候'
				})
				
				
				//php登录
				_this.$httpSend({
					path: '/im/in/login',
					data: {
						username: _this.form.username,
						password: _this.form.password,
						clientid: _this.form.clientid
						// clientid:'170976fa8b817e41070'
					},
					success: (data) => {
						
						let im = data;
						
						_this.$httpJavaSend({
							data: {
								apiname: 'userLOGIN',
								token: '',
								code: 100,
								apiparam: {
									usertab: 'OBSLIST',
									username: _this.form.username,
									password: _this.form.password,
								}
							},
							success(data) {
								if(data.msgcode == 0) {
									_data.localData('obs_token',data.token);
									_data.localData('obscode',data.obscode)
									_data.localData('phone',data.username)
									_data.localData('name',data.name)
									_data.localData('userid',im.userid)
									_data.localData('username',_this.form.username)
									_data.localData('password',_this.form.password)
									_custom.getDataJava({
										apiname: 'getUserObsList',
										apiparam: {
											username: data.username,
										},
										success(v) {
											v.forEach(item => {
												if(item.OBSCODE == data.obscode) {
													_data.localData('obsname',item.aname)
												}
											})
											uni.setStorage({ 
												key: 'token',
												data: im.token,
												fail: () => {
													uni.hideLoading();
													uni.showModal({
														content: '本地存储数据不可用!',
													});
												},
												success(){
													if(_this.form.password == '123456') {
														uni.hideLoading();
														_this.pwdVisible = true;
														return;
													}
													_data.localData('isLogOut',2);
													setTimeout(() => {
														uni.hideLoading();
														uni.reLaunch({
															url: '../home/index',
														});
													},1000)
												},
											});
										},
										fail() {
											uni.hideLoading();
											uni.showToast({
												title: "登录失败",
												icon: 'none'
											})
										}
									})
								}
							},
							fail(err) {
								console.log(err);
							}
						})						
						
						//EOD登录
						/*
						_this.$custom_httpSend({
							data: {
								apiname: 'userLOGIN',
								token: '',
								code: 100,
								apiparam: {
									usertab: 'OBSLIST',
									username: _this.form.username,
									password: _this.form.password,
								}
							},
							success(data) {
								if(data.msgcode == 0) {
									_data.localData('obs_token',data.token);
									_data.localData('obscode',data.obscode)
									_data.localData('phone',data.username)
									_data.localData('name',data.name)
									_data.localData('userid',im.userid)
									_data.localData('username',_this.form.username)
									_data.localData('password',_this.form.password)
									// #ifdef APP-PLUS
									//上传日志
									_custom.getDataJava({
										apiname: 'addAppLog',
										code: 3,
										apiparam: {
											 phonenum: data.username,
								             username: data.name,
								             Phoneid: plus.device.uuid
										}
									})
									// #endif
									_custom.getDataJava({
										apiname: 'getUserObsList',
										apiparam: {
											username: data.username,
										},
										success(v) {
											v.forEach(item => {
												if(item.OBSCODE == data.obscode) {
													_data.localData('obsname',item.aname)
												}
											})
											uni.setStorage({ 
												key: 'token',
												data: im.token,
												fail: () => {
													uni.hideLoading();
													uni.showModal({
														content: '本地存储数据不可用!',
													});
												},
												success(){
													if(_this.form.password == '123456') {
														uni.hideLoading();
														_this.pwdVisible = true;
														return;
													}
													_data.localData('isLogOut',2);
													setTimeout(() => {
														uni.hideLoading();
														uni.reLaunch({
															url: '../home/index',
														});
													},1000)
												},
											});
										},
										fail() {
											uni.hideLoading();
											uni.showToast({
												title: "登录失败",
												icon: 'none'
											})
										}
									})
								}
							},
							fail() {
								uni.hideLoading();
								uni.showToast({
									title: "登录失败",
									icon: 'none'
								})
							}
						})
						*/
						
						
						
					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							title: "登录失败",
							icon: 'none'
						})
					}
				});
			},
			/**
			 * 重置密码
			 */
			resetPwd() {
				let that = this;
				// this.form.clientid = '170976fa8b817e41070'
				if(!this.form.clientid) {
					uni.showToast({
						title: "未获取到client_id，请重启应用",
						icon: 'none'
					})
					return
				}
				this.$httpSend({
					path: '/im/in/preResetPwdCheck',
					data: {
						client_id: this.form.clientid,
						username: ''
					},
					success(e) {
						console.log(e)
						switch(e.num) {
							case 0:
								uni.showToast({
									title: "未绑定过设备，请直接登录",
									icon: 'none'
								})
								break;
							case 1:
								that.type = 1;
								that.pwdVisible = true;
								that.preResetData = e;
								break;
							default:
								that.type = 2;
								that.pwdVisible = true;
								break;
						}
					}
				})
			},
			go_forget(){
				uni.navigateTo({
				    url: '../../pages/in/forget'
				})
			},
			go_frozen(){
			uni.navigateTo({
				url: '../../pages/set/frozen'
			})	
			},
			go_register(){
				uni.navigateTo({
					url: '../../pages/in/reg'
				})
			}
			
		}
	}
</script>

<style>
	page {
		background: #fff;
	}
	.header {
		height: 100upx;
		text-align: center;
		padding: 0 40upx;
	}
	.header .goBack {
		width: 40upx;
		height: 100upx;
		background-image: url(/static/theme/default/login/goBack.png);
		background-size: auto 30upx;
		background-repeat: no-repeat;
		background-position: center;
	}
	.titleText {
		padding: 60upx;
	}
	.titleText .hy{
		font-family: PingFang SC;
		font-style: normal;
		font-weight: normal;
		font-size: 56upx;
		line-height: 78upx;
		color: #333333;
		mix-blend-mode: normal;
		margin-top: 80upx;
		}
	.titleText .xccp{
		font-family: PingFang SC;
		font-style: normal;
		font-weight: normal;
		font-size: 28upx;
		line-height: 40upx;
		color: #999999;
		mix-blend-mode: normal;
		margin-top: 30upx;
	}
	.uni-media-image{
		padding: 10upx 10upx;
		margin-top: 10upx;
		text-align: center;
	}
	.uni-media-loading {
		width:150upx;
		height:150upx;
		
	}
	.landing {
		height: 84upx;
		line-height: 84upx;
		width: 548upx;
		color: #FFFFFF;
		font-size: 32upx;
		bordor: none;
		border-radius: 10upx;
	}
	.placeholdertext{
		/* #ifdef H5 */
		width: 40upx;
		/* #endif */
		
		/*#ifdef APP-PLUS */
		width: 40upx;
		/* #endif */
		
		height:24upx;
	}
	.landing_true {
		
	}
	.landing_false {
		background: linear-gradient(278.07deg, #467CF6 0.25%, #6C99FF 100%);
		border-radius: 4px;
	}
	.uni-button[type=primary] {
		
	}
	.login-btn{
		padding: 10upx 20upx;
		/* margin-top: 68upx; */
		text-align: center;
	}
	.login-function{
		
		/* #ifdef H5 */
		margin-top:350upx;
		/* #endif */
		
		/*#ifdef APP-PLUS */
		margin-top: 260upx;
		
		
		/* #endif */
		
		color: #999;
		text-align: center;
	}
	.login-function-old{
		margin-top:40upx; 
		text-align: center;
	}
	.login-forget{
		float: left;
		font-size: 26upx;
		color: #999;
	}
	.textspace {
		padding: 10upx 10upx;
	}
	.login-register{
		width: 100%;
		text-align: center;
		color: #467CF6;
		font-size: 26upx;

	}
	.login-input input{
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}
	.login-margin-b{
		margin-bottom: 25upx;
	}
	.login-input{
		padding: 20upx 20upx;
	}
	.login-head{
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}
	.login-card{
		border-radius: 12upx;
		padding: 10upx 60upx;
		/* box-shadow: 0 6upx 18upx rgba(0,0,0,0.12); */
		position: relative;
	}
	.login-bg {
		/* height: 260upx;
		padding: 25upx;
		background: linear-gradient(#FF978D, #FFBB69); */
		background-image: url(/static/theme/default/login/bg.png);
		background-repeat: no-repeat;
		background-size: 45% auto;
		background-position: top right;
	}
	
	.content {
		background: #fff;
	}
	.uni-input {
		height: 50upx;
		width: 460upx;
		padding: 15upx 0 15upx 0;
		line-height:50upx;
		font-size:28upx;
		background:#FFF;
		flex: 1;
	}
	.uni-form-item .with-fun .uni-icon {
		text-align: left;
	}
	.uni-list-cell:after{display:none !important;}
	
	.pwd-box {
		width: 600rpx;
	}
	
	.pwd-box-item {
		padding: 0;
		padding-top: 10rpx;
		box-sizing: border-box;
	}
	
	.pwd-box__header {
		height: 100rpx;
		font-size: 30rpx;
		line-height: 100rpx;
		background-color: #eee;
		border-radius: 10rpx 10rpx 0 0;
		padding-left: 10rpx;
	}
	
	.pwd-box-item-title {
		padding: 0 10rpx;
	}
	
	.pwd-input {
		background-color: #eee;
		padding-left: 10rpx;
	}
	
	.reset {
		padding: 30upx 60upx;
		font-size: 28rpx;
		color: #aaa;
		display: flex;
		justify-content: flex-end;
	}
	
	.reset > view {
		border-bottom: 1rpx solid #eee;
	}
</style>
	